package microBlogger.screens;

import context.*;

import java.util.Enumeration;
import java.util.Hashtable;


import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import microBlogger.MicroBlogger;
//import javax.microedition.lcdui.Ticker;

import bloggerAPI.Blog;
import bloggerAPI.j2me.BloggerConnection;

// ******************************************************************
// *** CLASS: BlogListScreen
// ******************************************************************
public class BlogListScreen extends List implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
  private static final Command exitCommand=new Command("Exit",Command.EXIT,1);
  private static final Command backCommand=new Command("Back",Command.BACK,2);

  private Display display;
  private Displayable prev;

  public BlogListScreen(final MicroBlogger midlet,final BloggerConnection bloggerConnection)
  { super("My Blogs",List.IMPLICIT);
    this.display=Display.getDisplay(midlet);
    if(DEBUG) Debug.println("New BlogListScreen");
    this.addCommand(exitCommand);
    this.addCommand(backCommand);
    this.setCommandListener(new CommandListener()
    { public void commandAction(Command c,Displayable s)
      { if(c==exitCommand) { midlet.exit(); }
        else if(c==backCommand) { display.setCurrent(prev); }
        else if(c==SELECT_COMMAND)
        { if(DEBUG) Debug.println("BlogListScreen.SELECT_COMMAND: "+c);
          String id=getString(getSelectedIndex());
          Blog blog=bloggerConnection.getUserBlog(id);
       	  EntryListScreen entryListScreen=new EntryListScreen(midlet,bloggerConnection,blog);
          entryListScreen.show();
        }
      }
    });
    Hashtable blogTable=bloggerConnection.getBlogTable();
    for(Enumeration e=blogTable.elements();e.hasMoreElements();)
    { Blog blog=(Blog)e.nextElement();
      this.append(blog.getTitle(),null);                
    }
  }
  
  public void show()
  { prev=display.getCurrent();
    display.setCurrent(this);
  }
  
}
